#include "key_nodelay.h"


//GPIO的初始化
void Key_Init(Key_Handler* hkey)
{
	if(hkey->Key_GPIOx == GPIOA)
		__HAL_RCC_GPIOA_CLK_ENABLE();
	else if(hkey->Key_GPIOx == GPIOB)		
		__HAL_RCC_GPIOB_CLK_ENABLE();
	else if(hkey->Key_GPIOx == GPIOC)	
		__HAL_RCC_GPIOC_CLK_ENABLE();
	
	GPIO_InitTypeDef GPIO_InitStruct = {0}; 

	GPIO_InitStruct.Pin = hkey->GPIO_PIN;
	GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
	GPIO_InitStruct.Pull = GPIO_PULLUP;
	HAL_GPIO_Init(hkey->Key_GPIOx, &GPIO_InitStruct);
	
	hkey->key_sta.flag = 0;
	hkey->key_sta.Key_cnt = 0;
}

uint8_t KeyScan(Key_Handler* hkey)
{
	uint8_t KeyNum = 0;
	
	if(HAL_GPIO_ReadPin(hkey->Key_GPIOx, hkey->GPIO_PIN) == GPIO_PIN_RESET)
	{
		hkey->key_sta.flag = 0;
		hkey->key_sta.Key_cnt = 0;
	}
	else if(!hkey->key_sta.flag)
	{
		hkey->key_sta.Key_cnt++;
		
		if(hkey->key_sta.Key_cnt > KEY_MAX_DELAY)
		{
			hkey->key_sta.Key_cnt = 0;
			hkey->key_sta.flag = 1;
			KeyNum = 1;
		}
	}
	return KeyNum;
}

void Key_Proc(void)
{
	
	
}

